﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;

namespace GaSaiGon.GUI
{
    public partial class frmQuanLyTaiKhoan : Form
    {
        public frmQuanLyTaiKhoan()
        {
            InitializeComponent();
        }
        public void LoadTaiKhoan()
        {
            TaiKhoanBUS TKBus = new TaiKhoanBUS();
            dataGridView.DataSource = TKBus.LoadTaiKhoan();
        }
        private void btnRefesh_Click(object sender, EventArgs e)
        {
            LoadTaiKhoan();
        }

        private void frmQuanLyTaiKhoan_Load(object sender, EventArgs e)
        {
            LoadTaiKhoan();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            frmThem_CapNhatTaiKhoan frm = new frmThem_CapNhatTaiKhoan();
            frm.Status = "Them";
            frm.Show();
            frm.FormClosed += new FormClosedEventHandler(frmQuanLyTaiKhoan_FormClosed);
        }
        private void btnXoa_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("Bạn có muốn xóa tài khoản này!", "", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            if (DialogResult.Yes == dr)
            {
                TaiKhoanBUS TKBUS = new TaiKhoanBUS();
                TaiKhoanDTO TKDTO = new TaiKhoanDTO();
                TKDTO.MaTaiKhoan = Convert.ToInt32(dataGridView.Rows[dataGridView.CurrentRow.Index].Cells[0].Value);
                TKBUS.XoaTaiKhoan(TKDTO);
                LoadTaiKhoan();
            }
            else
            {
                return;
            }
        }
        private void btnCapNhat_Click(object sender, EventArgs e)
        {
            frmThem_CapNhatTaiKhoan frm = new frmThem_CapNhatTaiKhoan();
            frm.Status = "CapNhat";
            frm.TK = new TaiKhoanDTO();
            frm.TK.MaTaiKhoan = Convert.ToInt32(dataGridView.Rows[dataGridView.CurrentRow.Index].Cells[0].Value);
            frm.TK.TenDangNhap = Convert.ToString(dataGridView.Rows[dataGridView.CurrentRow.Index].Cells[1].Value);
            frm.TK.MatKhau = Convert.ToString(dataGridView.Rows[dataGridView.CurrentRow.Index].Cells[2].Value);
            frm.TK.HoTen = Convert.ToString(dataGridView.Rows[dataGridView.CurrentRow.Index].Cells[3].Value);
            frm.TK.Cmnd = Convert.ToString(dataGridView.Rows[dataGridView.CurrentRow.Index].Cells[4].Value);
            frm.TK.NgaySinh = Convert.ToDateTime(dataGridView.Rows[dataGridView.CurrentRow.Index].Cells[5].Value);
            frm.TK.GioiTinh = Convert.ToInt32(dataGridView.Rows[dataGridView.CurrentRow.Index].Cells[6].Value);
            frm.TK.DiaChi = Convert.ToString(dataGridView.Rows[dataGridView.CurrentRow.Index].Cells[7].Value);
            frm.TK.DienThoai = Convert.ToString(dataGridView.Rows[dataGridView.CurrentRow.Index].Cells[8].Value);
            frm.TK.ChucVu = Convert.ToInt32(dataGridView.Rows[dataGridView.CurrentRow.Index].Cells[9].Value);
            frm.Show();
            frm.FormClosed += new FormClosedEventHandler(frmQuanLyTaiKhoan_FormClosed);
        }
        private void txtTimKiem_TextChanged(object sender, EventArgs e)
        {
            if (txtTimKiem.Text == "")
                LoadTaiKhoan();
            else
            {
                TaiKhoanBUS TKBUS = new TaiKhoanBUS();
                TaiKhoanDTO TKDTO = new TaiKhoanDTO();
                TKDTO.TenDangNhap = txtTimKiem.Text;
                dataGridView.DataSource = TKBUS.TimKiemTaiKhoan(TKDTO);

            }
        }

        private void frmQuanLyTaiKhoan_FormClosed(object sender, FormClosedEventArgs e)
        {
            LoadTaiKhoan();
        }

        

    }
}
